package com.ldc.api.common;

import com.ldc.common.utils.DateTimeUtils;
import com.ldc.common.utils.FileUtils;
import com.ldc.common.utils.RequestUtils;
import com.ldc.model.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能说明
 *
 * @auther liudc
 * @since 2023/7/1
 */
public class ParamsHolder {
    ParamsHolder() {
        throw new IllegalStateException("Illegally created");
    }

    /**
     * 创建新用户
     *
     * @param userName
     * @param passWord
     * @return
     */
    public static SysUser createNewUser(String userName, String passWord) {
        long time = DateTimeUtils.getTime();
        SysUser sysUser = new SysUser();
        sysUser.setUserName(userName);
        sysUser.setPassWord(passWord);
        sysUser.setEnabled(true);
        sysUser.setHasAccountNonExpired(true);
        sysUser.setHasAccountNonLock(true);
        sysUser.setHasCredentialNonExpired(true);
        sysUser.setCreateTime(time);
        sysUser.setChangeTime(time);
        return sysUser;
    }

    /**
     * 创建用户信息
     *
     * @param sysUser
     * @return
     */
    public static UserInfo createNewUserInfo(SysUser sysUser) {
        long time = DateTimeUtils.getTime();
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(sysUser.getId());
        userInfo.setUserName(sysUser.getUserName());
        userInfo.setCreateTime(time);
        userInfo.setChangeTime(time);
        return userInfo;
    }

    /**
     * 创建新角色
     *
     * @param role
     * @param roleName
     * @param remark
     * @return
     */
    public static SysRole createNewRole(String role, String roleName, String remark) {
        long time = DateTimeUtils.getTime();
        SysRole sysRole = new SysRole();
        sysRole.setRole(role);
        sysRole.setRoleName(roleName);
        sysRole.setRemark(remark);
        sysRole.setChangeTime(time);
        sysRole.setCreateTime(time);
        return sysRole;
    }


    public static Article createNewArticle(Article articleParams) {
        Article article = new Article();
        if (articleParams == null) {
            return article;
        }
        article.setTitle(articleParams.getTitle());
        article.setContent(articleParams.getContent());
        article.setContentUrl(articleParams.getContentUrl());
        article.setTags(articleParams.getTags());
        article.setPics(articleParams.getPics());
        article.setParentType(articleParams.getParentType());
        article.setType(articleParams.getType());
        article.setAuthor(articleParams.getAuthor());
        return article;
    }


    public static File createNewFile(HttpServletRequest request, String filePath, String fileName, long fileSize) {
        String userId = RequestUtils.getUserId(request);
        String userName = RequestUtils.getUserName(request);
        long time = DateTimeUtils.getTime();
        String type = FileUtils.getFileExtension(filePath);
        File newFile = new File();
        newFile.setCreator(userId);
        newFile.setCreatorName(userName);
        newFile.setUrl(filePath);
        newFile.setName(fileName);
        newFile.setSize(fileSize);
        newFile.setType(type);
        newFile.setDelete(true);
        newFile.setDownload(true);
        newFile.setCreateTime(time);
        newFile.setChangeTime(time);
        return newFile;
    }
}
